package org.transaction.test.global_transaction.mybatis.service.impl;

import org.apache.ibatis.session.SqlSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.support.TransactionSynchronizationManager;
import org.transaction.test.global_transaction.mybatis.bean.User1;
import org.transaction.test.global_transaction.mybatis.mapper1.User1Mapper;
import org.transaction.test.global_transaction.mybatis.service.User1Service;

@Service
public class User1ServiceImpl implements User1Service {
	@Autowired
	private User1Mapper user1Mapper;
	
	public void truncate(){
		user1Mapper.truncated();
	}
	
	@Override
	public void add(User1 user){
		user1Mapper.insert(user);
	}
	
	@Override
	public void addException(User1 user){
		user1Mapper.insert(user);
		throw new RuntimeException();
	}

	// 使用传递的 SqlSession
	public void add(User1 user, SqlSession sqlSession) {
		User1Mapper mapper = sqlSession.getMapper(User1Mapper.class);
		mapper.insert(user);
	}

	// 使用传递的 SqlSession
	public void addException(User1 user, SqlSession sqlSession) {
		User1Mapper mapper = sqlSession.getMapper(User1Mapper.class);
		mapper.insert(user);
		throw new RuntimeException("通过sqlSession控制，故意抛出异常");
	}


	@Override
//	@Transactional(propagation = Propagation.REQUIRED)
	@Transactional(propagation = Propagation.REQUIRED,value = "user1TM")
	public void addRequired(User1 user){
		user1Mapper.insert(user);
	}
	
	@Override
//	@Transactional(propagation = Propagation.REQUIRED)
	@Transactional(propagation = Propagation.REQUIRED,value = "user1TM")
	public void addRequiredException(User1 user){
		user1Mapper.insert(user);
		throw new RuntimeException("内部方法异常User1 addRequiredException");
	}
	
	@Override
	@Transactional(propagation = Propagation.SUPPORTS)
	public void addSupports(User1 user){
		user1Mapper.insert(user);
	}
	
	@Override
	@Transactional(propagation = Propagation.SUPPORTS)
	public void addSupportsException(User1 user){
		user1Mapper.insert(user);
		throw new RuntimeException();
	}
	
	@Override
	@Transactional(propagation = Propagation.REQUIRES_NEW)
	public void addRequiresNew(User1 user){
		user1Mapper.insert(user);
	}
	
	@Override
	@Transactional(propagation = Propagation.REQUIRES_NEW)
	public void addRequiresNewException(User1 user){
		user1Mapper.insert(user);
		throw new RuntimeException();
	}
	
	@Override
	@Transactional(propagation = Propagation.NOT_SUPPORTED)
	public void addNotSupported(User1 user){
		user1Mapper.insert(user);
	}
	
	@Override
	@Transactional(propagation = Propagation.NOT_SUPPORTED)
	public void addNotSupportedException(User1 user){
		user1Mapper.insert(user);
		throw new RuntimeException();
	}
	
	@Override
	@Transactional(propagation = Propagation.MANDATORY)
	public void addMandatory(User1 user){
		user1Mapper.insert(user);
	}
	
	@Override
	@Transactional(propagation = Propagation.MANDATORY)
	public void addMandatoryException(User1 user){
		user1Mapper.insert(user);
		throw new RuntimeException();
	}
	
	@Override
	@Transactional(propagation = Propagation.NEVER)
	public void addNever(User1 user){
		user1Mapper.insert(user);
	}
	
	@Override
	@Transactional(propagation = Propagation.NEVER)
	public void addNeverException(User1 user){
		user1Mapper.insert(user);
		throw new RuntimeException();
	}
	
	@Override
	@Transactional(propagation = Propagation.NESTED)
	public void addNested(User1 user){
		user1Mapper.insert(user);
	}
	
	@Override
	@Transactional(propagation = Propagation.NESTED)
	public void addNestedException(User1 user){
		user1Mapper.insert(user);
		throw new RuntimeException();
	}
}
